﻿//-------------------------------------
// WBFSSync - WBFSSync.exe
//
// Copyright 2009 Caian (ÔmΣga Frøst) <frost.omega@hotmail.com> :
//
// WBFSSync is Licensed under the terms of the Microsoft Reciprocal License (Ms-RL)
//
// AeroMenuPanel.cs
//
// Classe que organiza os botões do menu
//
//-------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WBFSSync.AeroControls
{
    public partial class AeroMenuPanel : UserControl
    {
        //----------------------- Variáveis

        List<Control> LeftControls = new List<Control>();
        List<Control> RightControls = new List<Control>();

        int leftMargin = 4;
        public Int32 LeftMargin { get { return leftMargin; } set { leftMargin = value; OrganizeControls(); } }

        int rightMargin = 4;
        public Int32 RightMargin { get { return rightMargin; } set { rightMargin = value; OrganizeControls(); } }

        //----------------------- Rotinas

        public AeroMenuPanel()
        {
            InitializeComponent();
        }

        public void AddControlLeft(Control c)
        {
            LeftControls.Add(c);
            c.Tag = 0;
            Controls.Add(c);
        }

        public void AddControlRight(Control c)
        {
            RightControls.Add(c);
            c.Tag = 1;
            Controls.Add(c);
        }

        public new void SuspendLayout()
        {
            base.SuspendLayout();
            foreach (Control c in Controls) c.SuspendLayout();
        }

        public new void ResumeLayout()
        {
            base.SuspendLayout();
            foreach (Control c in Controls) c.ResumeLayout();
        }

        protected override void OnControlAdded(ControlEventArgs e)
        {
            e.Control.Top = 3; //Altura padrão de um botão do menu
            
            if (e.Control is AeroMenuButton)
            {
                (e.Control as AeroMenuButton).SizeChanged += new EventHandler(AeroMenuPanel_SizeChanged);
            }

            base.OnControlAdded(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            OrganizeControls(false, true);
            base.OnSizeChanged(e);
        }

        void AeroMenuPanel_SizeChanged(object sender, EventArgs e)
        {
            OrganizeControls((int)((sender as Control).Tag) == 0, (int)((sender as Control).Tag) == 1);
        }

        protected override void OnControlRemoved(ControlEventArgs e)
        {
            if (LeftControls.Contains(e.Control)) LeftControls.Remove(e.Control);
            else RightControls.Remove(e.Control);

            base.OnControlRemoved(e);
        }

        public void OrganizeControls()
        {
            OrganizeControls(true, true);
        }

        public void OrganizeControls(bool left, bool right)
        {
            SuspendLayout();

            if ((LeftControls.Count > 0) && left)
            {
                int pleft = leftMargin;
                for (int i = 0; i < LeftControls.Count; i++)
                {
                    if (!LeftControls[i].Visible) continue;
                    LeftControls[i].Left = pleft;
                    pleft += LeftControls[i].Width;
                }
            }

            if ((RightControls.Count > 0) && right)
            {
                int pleft = Width - leftMargin;
                for (int i = 0; i < RightControls.Count; i++)
                {
                    if (!RightControls[i].Visible) continue;
                    pleft -= RightControls[i].Width;
                    RightControls[i].Left = pleft;
                }

            }

            ResumeLayout();
        }
    }
}
